package cn.iocoder.yudao.module.handguard.controller.app.wechat;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.handguard.service.wechat.WeChatService;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.*;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

@Tag(name = "小程序 - 微信生成链接")
@RestController
@RequestMapping("/wechat")
@Validated
public class WeChatController {

    @Resource
    private WeChatService weChatService;

    /**
     * 生成二维码
     */
    @PostMapping("/createQRCode")
    @Operation(summary = "生成小程序推荐二维码")
    public CommonResult<Map> createQRCode() {
        //当前用户id
        Long userId = getLoginUser().getId();
        String path = "pages/index/index";
        Map<String,Object> map = weChatService.getWechatMiniRecommendScanUrl(userId,path);
        return CommonResult.success(map);
    }

    @GetMapping("/check")
    @Parameter(name = "code", description = "二维码参数（scene）", required = true)
    @Operation(summary = "绑定推荐人时-检查当前用户是否符合绑定条件")
    public CommonResult<Map> getCheckBinding(@RequestParam("code") Long code) {
        //当前用户id
        Long userId = getLoginUser().getId();
        Map<String,Object> map = weChatService.getCheckBinding(userId,code);
        return CommonResult.success(map);
    }

    @PostMapping("/affirm")
    @Parameter(name = "code", description = "二维码参数（scene）", required = true)
    @Operation(summary = "绑定推荐人-确认")
    public CommonResult<Map> affirm(@RequestParam("code") Long code) {
        //当前用户id
        Long userId = getLoginUser().getId();
        Map<String,Object> map = weChatService.affirm(userId,code);
        return CommonResult.success(map);
    }


}
